package Opter;

import Libray.BookShelf;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xwl
 * Date: 2023-04-16
 * Time: 17:48
 */
public class DeleteOpter implements IOpter{
    @Override
    public void opterFun(BookShelf bookShelf, String name) {
        // 定义位置
        int i = 0;
        for (i = 0; i < bookShelf.getBookNum() - 1; i++) {
            if (bookShelf.getBooks()[i].getBookName().equals(name)) {
                for (int j = i; j < bookShelf.getBookNum() - 1; j++) {
                    bookShelf.getBooks()[i] = bookShelf.getBooks()[i+1];
                }
                System.out.println("删除成功");
                break;
            }
        }
        //
        if (i == bookShelf.getBookNum() - 1
                && !bookShelf.getBooks()[bookShelf.getBookNum() - 1].getBookName().equals(name)) {
            System.out.println("删除失败，请重新选择");
        } else  if (bookShelf.getBooks()[bookShelf.getBookNum() - 1].getBookName().equals(name)){
            System.out.println("删除成功");
            bookShelf.getBooks()[bookShelf.getBookNum() - 1] = null;
            bookShelf.setBookNum(bookShelf.getBookNum() - 1);
        } else {
            bookShelf.getBooks()[bookShelf.getBookNum() - 1] = null;
            bookShelf.setBookNum(bookShelf.getBookNum() - 1);
        }
    }
}
